<template>
  <div class="">
    <el-row>
      <el-col :span="24">
        <div class="page-filter mb10 fl">
          <el-form ref="filter" :model="filter" label-width="0">
            <el-form-item class="mr10 mb5 fl" prop="begin_time">
              <el-date-picker
                v-model="filter.begin_time"
                placeholder="开始时间"
                type="date"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                size="small"
                clearable
                style="width: 100%"
              />
            </el-form-item>
            <el-form-item class="mr10 mb5 fl" prop="end_time">
              <el-date-picker
                v-model="filter.end_time"
                placeholder="结束时间"
                type="date"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                size="small"
                clearable
                style="width: 100%"
              />
            </el-form-item>
            <el-form-item class="mr10 mb5 fl" style="width: 300px">
              <el-button size="small" @click="app.resetFields($refs['filter'].$parent,'filter')">清 空</el-button>
              <el-button type="primary" size="small" @click="getList">查 询</el-button>
            </el-form-item>
            <el-form-item class="mr10 mb0 fl tal" style="width: 100%" label-width="85px" label="采购类别：">
              <el-checkbox-group v-model="filter.types">
                <el-checkbox v-for="item in types" :key="item.v" :label="item.v">{{ item.l }}</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
            <el-form-item class="mr10 mb5 fl tal" style="width: 100%" label-width="85px" label="统计纬度：">
              <el-radio v-for="item in dts" :key="item.v" v-model="filter.dt" :label="item.v">{{ item.l }}</el-radio>
            </el-form-item>
          </el-form>
        </div>
        <el-table
          v-loading="app.loading"
          class="search-table"
          :data="list"
          border
          :summary-method="getSummaries"
          show-summary
          fit
          highlight-current-row
          style="width: 100%;"
        >
          <el-table-column label="序号" align="center" width="56px">
            <template slot-scope="{$index}">
              {{ $index + 1 }}
            </template>
          </el-table-column>
          <el-table-column label="签订公司" min-width="220px" prop="entrust_name" />
          <el-table-column label="项目名称" min-width="220px" prop="pro_name" />
          <el-table-column label="采购日期" align="center" width="140px" prop="pur_time" />
          <el-table-column label="供应商名称" min-width="220px" prop="sup_name" />
          <el-table-column label="采购金额" align="right" width="140px">
            <template slot-scope="{row}">
              {{ app.toFix(row.price, 2) }}
            </template>
          </el-table-column>
          <el-table-column label="追加金额" align="right" width="140px">
            <template slot-scope="{row}">
              {{ app.toFix(row.additional, 2) }}
            </template>
          </el-table-column>
          <el-table-column label="追减金额" align="right" width="140px">
            <template slot-scope="{row}">
              {{ app.toFix(row.reduction, 2) }}
            </template>
          </el-table-column>
          <el-table-column label="已开票金额" align="right" width="140px">
            <template slot-scope="{row}">
              {{ app.toFix(row.has_tax, 2) }}
            </template>
          </el-table-column>
          <el-table-column label="未开票金额" align="right" width="140px">
            <template slot-scope="{row}">
              {{ app.toFix(app.toFix(row.price, 2) - app.toFix(row.has_tax, 2), 2) }}
            </template>
          </el-table-column>
          <el-table-column label="付款信息" align="right" width="210px">
            <template slot-scope="{row}">
              <span v-html="payInfo(row)" />
            </template>
          </el-table-column>
          <el-table-column label="发票" width="220px" prop="" />
          <el-table-column label="备注" min-width="220px" prop="remark" />
        </el-table>
        <pagination v-show="filter.pageAll>0" :total="filter.pageAll" :page.sync="filter.pageCurrent" :limit.sync="filter.pageNumber" @pagination="getList" />
      </el-col>
      <el-col :span="12" class="mt20">
        <div id="ba" style="width: 95%;height:400px;" />
      </el-col>
      <el-col :span="12" class="mt20">
        <div id="bn" style="width: 95%;height:400px;" />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import { purchaseNum, purchaseAmount, purchases, purchaseTotalAmount } from '@/api/statistics/statistics'
import page from '@/utils/page'

export default {
  name: '',
  components: { },
  extends: page,
  props: {
    reqList: {
      default: purchases,
      type: Function
    }
  },
  data() {
    return {
      parameter: { state: 1 },
      price: {},
      price_summary: {},
      types: [{ v: 1, l: '物料' }, { v: 2, l: '设备' }, { v: 3, l: '工程外包' }, { v: 4, l: '仓库' }],
      dts: [{ v: 'y', l: '年份' }, { v: 'm', l: '月份' }, { v: 'd', l: '日期' }],
      filter: {
        dt: 'm',
        types: [1, 2, 3, 4]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    payInfo(e) {
      if (e && e.pay_info) {
      //   2022-12-30#900.00
      // console.log(e);
        const infos = e.pay_info.split(',')
        const len = infos.length
        let sd = ''
        for (let i = 0; i < len; i++) {
          const ps = infos[i].split('#')
          sd += ps[0] || ''
          sd += '; 金额:'
          sd += ps[1] || ''
          sd += '<br/>'
        }
        return sd
      } else {
        return
      }
    },
    getListAfter(e) {
      this.price_summary = e.summary
      this.handleAmount()
      this.handleSNum()
      this.handleSAmount()
    },
    handleAmount() {
      purchaseTotalAmount({
        ...this.filter
      }).then(response => {
        this.price = response.info ? response.info : {}
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    handleSAmount() {
      purchaseAmount({
        ...this.filter
      }).then(response => {
        const x = []
        const y = []
        const amounts = response.list ? response.list : []
        if (amounts.length > 0 && this.filter.types.length > 0) {
          amounts.forEach(e => {
            x.push(e.dt || '')
            y.push(e.num || 0)
          })
        }

        var chartDom = document.getElementById('ba')
        var myChart = echarts.init(chartDom)
        var option

        option = {
          title: {
            text: '采购金额统计',
            subtext: ''
          },
          grid: {
            top: 50,
            left: '2%',
            right: '2%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: {
            type: 'category',
            data: x,
            axisLabel: {
              interval: 0,
              rotate: 20// 角度顺时针计算的
            }
          },
          yAxis: {
            type: 'value'
          },
          series: [
            {
              data: y,
              type: 'bar',
              showBackground: true,
              label: {
                show: true,
                position: 'inside'
              },
              backgroundStyle: {
                color: 'rgba(180, 180, 180, 0.2)'
              }
            }
          ]
        }
        option && myChart.setOption(option)
      }).catch(err => {
        this.$message.error(err.msg ? err.msg : '获取数据失败')
      })
    },
    handleSNum() {
      purchaseNum({
        ...this.filter
      }).then(response => {
        const x = []
        const y = []
        const nums = response.list ? response.list : []
        if (nums.length > 0 && this.filter.types.length > 0) {
          nums.forEach(e => {
            x.push(e.dt || '')
            y.push(e.num || 0)
          })
        }

        var chartDom = document.getElementById('bn')
        var myChart = echarts.init(chartDom)
        var option

        option = {
          title: {
            text: '采购个数统计',
            subtext: ''
          },
          grid: {
            top: 50,
            left: '2%',
            right: '2%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: {
            type: 'category',
            data: x,
            axisLabel: {
              interval: 0,
              rotate: 20// 角度顺时针计算的
            }
          },
          yAxis: {
            type: 'value'
          },
          series: [
            {
              data: y,
              type: 'bar',
              showBackground: true,
              label: {
                show: true,
                position: 'inside'
              },
              backgroundStyle: {
                color: 'rgba(180, 180, 180, 0.2)'
              }
            }
          ]
        }
        option && myChart.setOption(option)
      }).catch(err => {
        this.$message.error(err.msg ? err.msg : '获取数据失败')
      })
    },
    getSummaries(param) {
      console.log(this.price_summary);
      const { columns, data } = param
      const sums = []
      sums[0] = '合计'
      sums[1] = ''
      sums[2] = ''
      sums[3] = ''
      sums[4] = ''
      sums[5] = this.app.toFix(this.price_summary.price, 2)
      sums[6] = this.app.toFix(this.price_summary.additional, 2)
      sums[7] = this.app.toFix(this.price_summary.reduction, 2)
      sums[8] = this.app.toFix(this.price_summary.has_tax, 2)
      sums[9] = this.app.toFix(this.price_summary.price - this.price_summary.has_tax, 2)
      return sums
    }
  }
}
</script>
